package com.example.shushiyunsst.fragment.main.home.recycler.provider;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.example.library_common.adapter.multi.JssMultiItemViewHolder;
import com.example.library_common.adapter.multi.JssNewMultipleAdapter;
import com.example.library_common.utils.QMUIDisplayHelper;
import com.example.shushiyunsst.R;
import com.example.shushiyunsst.fragment.main.home.recycler.bean.BannerEntity;
import com.example.shushiyunsst.fragment.main.home.recycler.bean.BannerEntityWrapper;
import com.example.wallet.global.AppGlobals;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;

public class BannerProvider extends JssMultiItemViewHolder<BannerEntityWrapper> {

    private BannerHomeAdapter mAdapter;

    private final int height;

    public BannerProvider(View view) {
        super(view);
        int screenWidth = QMUIDisplayHelper.getScreenWidth(AppGlobals.getApplication());
        height = screenWidth / 2;
    }

    @Override
    public int getItemLayout() {
        return R.layout.banner_provider_layout;
    }

    @Override
    public void convert(Context context, BannerEntityWrapper bannerEntity) {
        super.convert(context, bannerEntity);
        Banner<BannerEntity, BannerHomeAdapter> mBannerView = findView(R.id.mBanner);
        if (mAdapter == null && mBannerView != null) {

            ViewGroup.LayoutParams lp = mBannerView.getLayoutParams();
            if (lp !=null&&lp.height!=height){
                lp.height=height;
                mBannerView.setLayoutParams(lp);
            }

            mAdapter = new BannerHomeAdapter(new ArrayList<>(), context);
            mBannerView.setAdapter(mAdapter);
            mBannerView.setIndicator(new CircleIndicator(context));
            if (adapter instanceof JssNewMultipleAdapter) {
                mBannerView.addBannerLifecycleObserver(((JssNewMultipleAdapter) adapter).getLifecycleOwner());
            }
        }
        if (mAdapter != null) {
            mAdapter.addAll(bannerEntity.getList());
        }
    }

    @Override
    public void onViewAttachedToWindow() {
        super.onViewAttachedToWindow();
        Banner<BannerEntity, BannerHomeAdapter> mBannerView = findView(R.id.mBanner);
        if (mBannerView != null) {
            mBannerView.start();
        }
    }

    @Override
    public void onViewDetachedFromWindow() {
        super.onViewDetachedFromWindow();
        Banner<BannerEntity, BannerHomeAdapter> mBannerView = findView(R.id.mBanner);
        if (mBannerView != null) {
            mBannerView.stop();
        }
    }
}
